import type { QuizInfo, AnswerItem, AnswerReportItem, UserAnswer, SingleItem } from '@/types/quiz'
import http from './http'
import { apiMsgProp } from '@/utils/global'
import type { QuestionItem } from '@/types/question'

const QuizService = {
    /**
     * @function getById 获取考考你详情
     * @param id 考考你id quiz
     * @returns QuizInfo
     */
    getById: async (id: number): Promise<QuizInfo> => {
        const res = await http.get(`/course/quiz/detail/${id}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data.items
    },
    /**
     * @function save 保存考考你详情
     * @param id 考考你id quiz
     */
    save: async (data: QuizInfo): Promise<void> => {
        const res = await http.post(`/course/resource/quiz/save`, data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    /**
     * @function records 获取作答记录
     * @param id 作答记录id
     * @returns AnswerItem[]
     */
    records: async (id: number): Promise<AnswerItem[]> => {
        const res = await http.get(`/course/quiz/${id}/userAnswerList`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data.items
    },
    /**
     * @function lastRecord 最后一次作答(继续上一次的预览/生成新的答卷)
     * @param id 考考你id quiz
     * @returns AnswerItem
     */
    lastRecord: async (id: number): Promise<AnswerItem> => {
        const res = await http.get(`/course/quiz/${id}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }

        let tag = false
        if (!res.data.data.items.user_answer.length) tag = true
        res.data.data.items.questions.forEach((item: any) => {
            item.answer = JSON.parse(item.answer)

            if (tag) {
                res.data.data.items.user_answer.push({
                    question_id: item.id,
                    selected_option_ids: item.question_type === 1 ? null : [],
                })
            }
        })
        return res.data.data.items
    },
    /**
     * @function recordById 獲取作答記錄詳情
     * @param id 答题记录id
     * @returns AnswerItem
     */
    recordById: async (id: number): Promise<AnswerItem> => {
        const res = await http.get(`/course/quiz/answer/${id}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        const answerInfo = res.data.data.items
        answerInfo.questions.forEach((item: any) => {
            item.answer = JSON.parse(item.answer)
            item.answer_feedback = JSON.parse(item.answer_feedback)
        })
        return answerInfo
    },
    /**
     * @function saveRecord 保存答卷/提交答卷
     * @param id record id 答题记录id
     * @param data action: save/submit; answers: 用户的答题数组
     * @returns AnswerItem
     */
    saveRecord: async (id: number, data: { action: string; answers: UserAnswer[] }): Promise<AnswerItem> => {
        const res = await http.post(`/course/quiz/${id}`, data)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data.items
    },
    /**
     * @function recordsToTeacher 老师端-获取作答记录
     * @param id 考考你id quiz
     * @param form 筛选条件
     * @returns AnswerReportItem[]
     */
    recordsToTeacher: async (id: number, form: any): Promise<{ items: AnswerReportItem[]; average_scores: any }> => {
        let apiUrl = `/course/quiz/${id}/answer-list?is_answer=${form.is_answer}`
        if (form.is_showHight) apiUrl += `&is_showHight=${form.is_showHight}`
        if (form.additional_last_name && form.additional_last_name !== 'all') apiUrl += `&additional_last_name=${form.additional_last_name}`
        if (form.additional_first_name && form.additional_first_name !== 'all') apiUrl += `&additional_first_name=${form.additional_first_name}`

        const res = await http.get(apiUrl)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function exportCSV 老师端 - 答卷记录导出
     * @param id quiz id
     * @returns
     */
    exportCSV: async (id: number, form: any): Promise<any> => {
        let apiUrl = `/course/quiz/${id}/export-csv?is_answer=${form.is_answer}`
        if (form.is_showHight) apiUrl += `&is_showHight=${form.is_showHight}`
        if (form.additional_last_name && form.additional_last_name !== 'all') apiUrl += `&additional_last_name=${form.additional_last_name}`
        if (form.additional_first_name && form.additional_first_name !== 'all') apiUrl += `&additional_first_name=${form.additional_first_name}`
        const res = await http.get(apiUrl)
        if (res.status !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data
    },
    /**
     * @function editScore 老师端 - 修改单题分数
     * @param id 考考你id quiz
     * @param question_id 题目 id
     * @param data 修改分数数据
     * @returns
     */
    editScore: async (id: number, question_id: number, data: { score: string; comment: string }): Promise<void> => {
        const res = await http.put(`/course/quiz/editScore?id=${id}&question_id=${question_id}`, data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
}

export default QuizService
